package net.kehuilai;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.concurrent.CountDownLatch;

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

    private SocketServer socketServer;

    @Bean
    public SocketServer socketServer() {
        return this.socketServer;
    }

    /* 启动Server */
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

    @PostConstruct
    public void init() {
        this.socketServer = new SocketServer("127.0.0.1", 20550);
        this.socketServer.start();
    }

    @PreDestroy
    public void destroy() {
        this.socketServer.close();
    }
}
